package org.easyexcel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**
 * @ClassName ConsumeRecordSheetWriteHandler
 * @Description
 * @Author c.w
 * @Date 2025/7/29 14:43
 * @Version 1.0.0
 */
public class ConsumeRecordSheetWriteHandler implements SheetWriteHandler {

	private CellStyle titleCellStyle;
	private CellStyle unitCellStyle;
	List<CellRangeAddress> mergeRangeList;

	public ConsumeRecordSheetWriteHandler(List<CellRangeAddress> mergeRangeList) {
		this.mergeRangeList = mergeRangeList;
	}

	@Override
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

	}

	// 设置标题
	@Override
	public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

		Workbook workbook = writeWorkbookHolder.getWorkbook();
		if (titleCellStyle == null) {
			titleCellStyle = workbook.createCellStyle();
			titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
			titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			titleCellStyle.setBorderTop(BorderStyle.THIN);
			titleCellStyle.setBorderRight(BorderStyle.THIN);
			titleCellStyle.setBorderBottom(BorderStyle.THIN);
			Font font = workbook.createFont();
			font.setBold(true);
			font.setFontName("微软雅黑");
			font.setFontHeightInPoints((short) 15);
			titleCellStyle.setFont(font);
		}
		if(unitCellStyle == null){
			unitCellStyle = workbook.createCellStyle();
			unitCellStyle.setAlignment(HorizontalAlignment.RIGHT);
			unitCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			unitCellStyle.setBorderTop(BorderStyle.THIN);
			unitCellStyle.setBorderRight(BorderStyle.THIN);
			unitCellStyle.setBorderBottom(BorderStyle.THIN);
			Font font = workbook.createFont();
			font.setBold(false);
			font.setFontName("微软雅黑");
			font.setFontHeightInPoints((short) 8);
			unitCellStyle.setFont(font);
		}
		Sheet sheet = workbook.getSheetAt(0);
		Row row1 = sheet.createRow(0);
		row1.setHeight((short) 800);
		Cell cell = row1.createCell(0);
		for (int i = 1; i <= 14; i++) {
			Cell cellOther = row1.createCell(i);
			cellOther.setCellStyle(titleCellStyle);
		}
		//设置标题
		cell.setCellValue("加油卡消费记录明细");
		sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 14));
		cell.setCellStyle(titleCellStyle);

		Row row2 = sheet.createRow(1);
		row2.setHeight((short) 400);
		Cell cell2 = row2.createCell(0);
		for (int i = 1; i <= 14; i++) {
			Cell cellOther = row2.createCell(i);
			cellOther.setCellStyle(unitCellStyle);
		}
		cell2.setCellValue("数量：L 价格：元   金额：元");
		sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 14));
		cell2.setCellStyle(unitCellStyle);

		// 添加合并区域
		if (mergeRangeList != null && !mergeRangeList.isEmpty()) {
			for (CellRangeAddress range : mergeRangeList) {
				// 添加偏移量，因为使用了.relativeHeadRowIndex(2)
				CellRangeAddress adjustedRange = new CellRangeAddress(
						range.getFirstRow() + 2,
						range.getLastRow() + 2,
						range.getFirstColumn(),
						range.getLastColumn()
				);
				sheet.addMergedRegionUnsafe(adjustedRange);
			}
		}
	}
}